﻿///Contact entity services library

(function (ns)
{
    //constants
    var Constants = {
        MSG_NO_RESULTS: "No results to display",
        MSG_GENERAL_FAILURE: "Something went wrong :( ",
        MSG_PROCESSING: "Working on it...",
        MSG_ERR_NO_CONTEXT: "No context found, operation aborted",
        ODATA_ENDPOINT_SUFFIX: "/api/data/v8.0/",
        FORMTYPE:
        {
            FORM_TYPE_CREATE: 1,
            FORM_TYPE_UPDATE: 2,
            FORM_TYPE_READONLY: 3
        }
    };

    //Members
    var ODATAEndPoint = null;

    //public methods                    

    //handle form load event 
    ns.HandleFormLoadEvent = function (context)
    {
        try
        {
            //call form onLoad event handlers
        }
        catch (error)
        {
            //handle error 
        }
    }

    //handle control change event 
    ns.HandleControlChangeEvent = function (context)
    {
        try
        {
            //if context was not supplied, extract it

            //extract source attribute name 
            var sourceAttributeName = context.getEventSource().getName();

            //direct event to actual event handler according to attribute name
            switch (sourceAttributeName)
            {
                case "birthdate":
                    //Call the actual event handling function here  
                    CalculateAge();
                    break;
                default:
                    break;
            }
        }
        catch (error)
        {
            //handle error 
        }
    }

    //handle form onload event 
    ns.HandleFormSaveEvent = function (context)
    {
        try
        {
            //if context, was not supplied, extract it

            //call form onSave event handlers
        }
        catch (error)
        {
            //handle error 
        }
    }

    //private methods

    //Calculate age according to birthdate     
    CalculateAge = function (birthDate)
    {
        var result = 0;

        var birthDate = Xrm.Page.getAttribute("birthdate").getValue();
        
        var birthday = +new Date(birthDate);

        result = ~~((Date.now() - birthday) / (31557600000));

        return result;
    }

})(window.ContactServices = window.ContactServices || {});
